package com.mycompany.j2eem.Aop;

import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author wucy
 */
@RestController
@ControllerAdvice(basePackages = "com.mycompany.j2eem.controller")
public class Error {

    @ExceptionHandler(Exception.class)
    public JsonResult errorResult(Exception e, HttpSession session) {
        if (e instanceof NoLoginException) {
            return new JsonResult(ResultCode.NOT_LOGIN);
        } else if (e instanceof NullPointerException) {
            String message = e.getMessage();
            if (message != null) {
                e.printStackTrace();
                return new JsonResult(ResultCode.FAIL, message,"");
            } else {
                e.printStackTrace();
                return new JsonResult(ResultCode.FAIL);
            }
        }else if(e instanceof MySQLIntegrityConstraintViolationException){
            e.printStackTrace();
            return new JsonResult(ResultCode.FAIL);
        }else{
              e.printStackTrace();
            return new JsonResult(ResultCode.UNKNOWN_ERROR);
        }

    }
}
